package cn.iver.common;

import cn.iver.component.GlobalEnhancer;
import cn.iver.component.GlobalInterceptor;
import cn.iver.component.MyHandler;
import cn.ratel.cache.CachePlugin;
import cn.ratel.cache.enhancer.CacheEnhancer;
import cn.ratel.cache.redis.RedisCacheProvider;
import cn.ratel.component.*;
import cn.ratel.db.ActiveRecordPlugin;
import cn.ratel.db.ds.DruidDataSourceProvider;
import cn.ratel.kit.PropKit;
import cn.ratel.route.RenderProvider;
import cn.ratel.route.render.template.HandlebarsRender;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by iver
 * on 2015/11/9
 */
public class Config extends Configuration {

	@Override
	public void configConstants(Constants constants) {
		constants.setDevMode(PropKit.getBoolean("const.dev", true));
		constants.setProfile("dev");
		constants.setScanPackage("cn.iver");

		HandlebarsRender handlebarsRender = new HandlebarsRender();
		RenderProvider.addRender(handlebarsRender);
	}

	@Override
	public void configHandlers(Handlers handlers) {
		handlers.addHandler(new MyHandler());
	}

	@Override
	public void configEnhancers(Enhancers enhancers) {
		enhancers.addGlobalEnhancer(new GlobalEnhancer());
		enhancers.addGlobalEnhancer(new CacheEnhancer());
		enhancers.addGlobalInterceptor(new GlobalInterceptor());
	}

	@Override
	public void configPlugins(Plugins plugins) {
		String redisHost = PropKit.get("redis.host");
		Integer redisPort = PropKit.getInt("redis.port");
		RedisCacheProvider cacheProvider = new RedisCacheProvider(new JedisPoolConfig() {
			{
				setMaxTotal(3);
			}
		}, redisHost, redisPort);
		new CachePlugin(cacheProvider).start();

//		plugins.addPlugin(new CachePlugin(new EhcacheCacheProvider("/tmp")));

		String url = PropKit.get("db.url");
		String username = PropKit.get("db.username");
		String password = PropKit.get("db.password");
		String driver = PropKit.get("db.driver");
		DruidDataSourceProvider dataSourceProvider = new DruidDataSourceProvider(url, username, password, driver);
		ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(dataSourceProvider);
		plugins.addPlugin(activeRecordPlugin);
	}
}
